﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TestGame
{
    public class Player : Sprite
    {
        private KeyboardState PreviousKeyboardState;
        private MouseState PreviousMouseState;

        //hud objects
        public List<HUD> hud = new List<HUD>();

        //directional constants
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int MOVE_LEFT = -1;
        const int MOVE_RIGHT = 1;

        public Sprite lastClicked { get; set; }

        public Texture2D mouseCursor { get; set; }

        public Player(int x, int y, float f, string s) : base(x, y, s, f) { }

        public override void Update(GameTime gametime)
        {
            if (CurrentState != State.Background)
            {
                //basically, this only works for players
                ManageKeyboardAndMouse();

                //change the players position
                Position += mDirection * mSpeed * (float)gametime.ElapsedGameTime.TotalSeconds;
            }
        }

        private void ManageKeyboardAndMouse()
        {
            // get new states
            KeyboardState CurrentKeyboardState = Keyboard.GetState();
            MouseState CurrentMouseState = Mouse.GetState();

            // move the camera based on keyboard states
            UpdateMovement(CurrentKeyboardState);
            CheckMouse(CurrentMouseState);

            // set current states to old ones
            PreviousKeyboardState = CurrentKeyboardState;
            PreviousMouseState = CurrentMouseState;
        }

        private void CheckMouse(MouseState CurrentMouseState)
        {
            Vector2 mouseHit = new Vector2(CurrentMouseState.X, CurrentMouseState.Y);

            // can't hit outside or above
            if (mouseHit.X < 0 || mouseHit.Y < 0) { return; }

            // TODO if(mousehit.x > windowmaxx, etc) { }

            // TODO if(customCursor) { }

            if (CurrentMouseState.LeftButton == ButtonState.Pressed && PreviousMouseState.LeftButton == ButtonState.Released)
            {
                lastClicked = null;

                //search through the highest layers first
                foreach (Sprite sprite in SpriteManager.Clickables.Reverse())
                {
                    if (sprite.Intersect(mouseHit))
                    {
                        lastClicked = sprite;
                        break;
                    }
                }

                // TODO reset mouseCursor
            }
        }

        private void UpdateMovement(KeyboardState CurrentKeyboardState)
        {
            // if something is moving
            if (CurrentState == State.Walking && CurrentState != State.Background)
            {
                // reset speed and direction
                mSpeed = Vector2.Zero;
                mDirection = Vector2.Zero;

                // supports WASD and arrow keys
                if (CurrentKeyboardState.IsKeyDown(Keys.Left) || CurrentKeyboardState.IsKeyDown(Keys.A))
                {
                    mSpeed.X = MOVE_SPEED;
                    mDirection.X = MOVE_LEFT;
                }
                else if (CurrentKeyboardState.IsKeyDown(Keys.Right) || CurrentKeyboardState.IsKeyDown(Keys.D))
                {
                    mSpeed.X = MOVE_SPEED;
                    mDirection.X = MOVE_RIGHT;
                }

                if (CurrentKeyboardState.IsKeyDown(Keys.Up) || CurrentKeyboardState.IsKeyDown(Keys.W))
                {
                    mSpeed.Y = MOVE_SPEED;
                    mDirection.Y = MOVE_UP;
                }
                else if (CurrentKeyboardState.IsKeyDown(Keys.Down) || CurrentKeyboardState.IsKeyDown(Keys.S))
                {
                    mSpeed.Y = MOVE_SPEED;
                    mDirection.Y = MOVE_DOWN;
                }
            }
        }
    }
}
